package com.wuwangfu.chain;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.Collector;

import java.util.Properties;

/**
 * @Author jcshen
 * @Date 2023-02-27
 * @PackageName:com.wuwangfu.framework
 * @ClassName: SubTaskChains
 * @Description:
 * @Version 1.0.0
 *
 * task的划分和subtask的数量问题
 *
 * https://nightlies.apache.org/flink/flink-docs-release-1.14/docs/concepts/flink-architecture/#tasks-and-operator-chains
 *
 */
public class SubTaskChains {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(2);
        //Kafka配置
        Properties prop = new Properties();
        prop.setProperty("bootstrap.servers","node03:9092");
        prop.setProperty("auto.offset.reset","earliest");
        prop.setProperty("group.id","stc");
        prop.setProperty("enable.auto.commit","true");
        //创建Kafka Consumer
        FlinkKafkaConsumer<String> kafkaConsumer = new FlinkKafkaConsumer<>("task", new SimpleStringSchema(), prop);
        //Source 并行度？ 2
        DataStreamSource<String> lines = env.addSource(kafkaConsumer);
        //生成watermark
//        SingleOutputStreamOperator<String> dataWithWatermark = lines.assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<String>(Time.seconds(0)) {
//            @Override
//            public long extractTimestamp(String element) {
//                String[] fields = element.split(",");
//                return Long.parseLong(element.split(",")[0]);
//            }
//        });
        //map
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndCount = lines.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                String[] fields = value.split(",");
                return Tuple2.of(fields[1], Integer.parseInt(fields[2]));
            }
        });
        //分组
        SingleOutputStreamOperator<Tuple2<String, Integer>> result = wordAndCount.keyBy(t -> t.f0)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .apply(new WindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String, TimeWindow>() {
                    @Override
                    public void apply(String key, TimeWindow window, Iterable<Tuple2<String, Integer>> input, Collector<Tuple2<String, Integer>> out) throws Exception {
                        for (Tuple2<String, Integer> tp : input) {
                            out.collect(tp);
                        }
                    }
                });
        //
        result.print().setParallelism(1);

        env.execute();
    }
}
